﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using DataBase;

namespace DBService
{
	public class DbFloor : DbObject
	{
		[Category("属性")]
		[Description("洞口集合")]
		[DisplayName("洞口集合")]
		[ReadOnly(true)]
		public List<DbOpening> OpeningList
		{
			get
			{
				return this.mOpeningList;
			}
		}

		[Category("属性")]
		[Description("建筑模型Id")]
		[DisplayName("建筑模型Id")]
		[ReadOnly(true)]
		public int RacId
		{
			get
			{
				return this.mRacId;
			}
			set
			{
				this.mRacId = value;
			}
		}

		[Category("属性")]
		[Description("结构模型Id")]
		[DisplayName("结构模型Id")]
		[ReadOnly(true)]
		public int RmeId
		{
			get
			{
				return this.mRmeId;
			}
			set
			{
				this.mRmeId = value;
			}
		}

		public DbFloor(int projectId, int racId, int rmeId, List<DbOpening> openingList)
		{
			this.mProjectId = projectId;
			this.mRacId = racId;
			this.mRmeId = rmeId;
			this.mOpeningList = openingList;
		}

		public DbFloor(int projectId, int racId, int rmeId)
		{
			this.mProjectId = projectId;
			this.mRacId = racId;
			this.mRmeId = rmeId;
		}

		public static DbFloor GetFromDB(int id, IDatabase db)
		{
			DbFloor result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT ProjectId,RacId,RmeId FROM tbfloor WHERE Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					int racId = Convert.ToInt32(dataTable.Rows[0]["RacId"]);
					int rmeId = Convert.ToInt32(dataTable.Rows[0]["RmeId"]);
					result = new DbFloor(Convert.ToInt32(dataTable.Rows[0]["ProjectId"]), racId, rmeId, DbFloor.GetOpeningsOfFloor(id, db))
					{
						mId = id,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private static List<DbOpening> GetOpeningsOfFloor(int floorId, IDatabase db)
		{
			List<DbOpening> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbOpening> list = new List<DbOpening>();
				string sql = string.Format("SELECT OpeningId FROM tbopeningoffloor WHERE FloorId={0}", floorId);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					DbOpening fromDB = DbOpening.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["OpeningId"]), db);
					list.Add(fromDB);
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbfloor (projectId, RacID,RmeID) VALUES ({0},{1},{2})", this.mProjectId, this.mRacId, this.mRmeId);
				db.ExecuteCommand(sql);
				this.mDatabase = db;
				string sql2 = string.Format("select max(Id) from tbslab", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				foreach (DbOpening dbOpening in this.mOpeningList)
				{
					dbOpening.AddToDb(db);
					if (db.Connection.State == ConnectionState.Closed)
					{
						db.Connection.Open();
					}
					string sql3 = string.Format("INSERT INTO tbopeningoffloor (FloorId, OpeningId) VALUES ({0},{1})", base.Id, dbOpening.Id);
					db.ExecuteCommand(sql3);
					if (db.Connection.State == ConnectionState.Open)
					{
						db.Connection.Close();
					}
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public void AddOpening(DbOpening opening)
		{
			try
			{
				opening.AddToDb(this.mDatabase);
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbopeningofloor (FloorId, OpeningId) VALUES ({0},{1})", base.Id, opening.Id);
				this.mDatabase.ExecuteCommand(sql);
				this.mOpeningList.Add(opening);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			throw new NotImplementedException();
		}

		public override void RemoveFromDb()
		{
			if (base.Database.Connection.State == ConnectionState.Closed)
			{
				base.Database.Connection.Open();
			}
			using (DbTransaction dbTransaction = base.Database.StartTransaction())
			{
				try
				{
					string sql = string.Format("DELETE FROM tbfloor WHERE Id = {0}", base.Id);
					base.Database.ExecuteCommand(sql, dbTransaction);
					foreach (DbOpening dbOpening in this.OpeningList)
					{
						dbOpening.RemoveFromDb();
						if (base.Database.Connection.State == ConnectionState.Closed)
						{
							base.Database.Connection.Open();
						}
						string sql2 = string.Format("DELETE FROM tbopeningoffloor WHERE FloorId={0}", base.Id);
						base.Database.ExecuteCommand(sql2, dbTransaction);
					}
					if (base.Database.Connection.State == ConnectionState.Closed)
					{
						base.Database.Connection.Open();
					}
					dbTransaction.Commit();
					if (base.Database.Connection.State == ConnectionState.Open)
					{
						base.Database.Connection.Close();
					}
				}
				catch (Exception ex)
				{
					if (base.Database.Connection.State == ConnectionState.Closed)
					{
						base.Database.Connection.Open();
					}
					dbTransaction.Rollback();
					if (base.Database.Connection.State == ConnectionState.Open)
					{
						base.Database.Connection.Close();
					}
					throw ex;
				}
			}
		}

		public static void Clear(DbProject project)
		{
			foreach (DbFloor dbFloor in DbFloor.GetByProject(project))
			{
				dbFloor.RemoveFromDb();
			}
		}

		public static List<DbFloor> GetByProject(DbProject project)
		{
			List<DbFloor> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbFloor> list = new List<DbFloor>();
				string sql = string.Format("SELECT ProjectId,RacId,RmeId,Id FROM tbfloor WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = list;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int racId = Convert.ToInt32(dataTable.Rows[0]["RacId"]);
						int rmeId = Convert.ToInt32(dataTable.Rows[0]["RmeId"]);
						int projectId = Convert.ToInt32(dataTable.Rows[0]["ProjectId"]);
						int num = Convert.ToInt32(dataTable.Rows[0]["Id"]);
						list.Add(new DbFloor(projectId, racId, rmeId, DbFloor.GetOpeningsOfFloor(num, project.Database))
						{
							mId = num,
							mDatabase = project.Database
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private int mProjectId;

		public List<DbOpening> mOpeningList = new List<DbOpening>();

		private int mRacId;

		private int mRmeId;
	}
}
